package adventuregame;

import java.util.ArrayList;
import java.util.Random;

/**
 * Contains information about a Room object, a list Things and a list of Neighbour rooms
 * @author Jesper & Simon
 */
public class Room {

	Console console = new Console();
	private String name = "";
	ArrayList<String> neighbours = new ArrayList<String>();
	ArrayList<Thing> things = new ArrayList<Thing>();
	String nameAddition = "";

	/**
	 * Initializes the Room object.
	 * adds nameAdditon (base), if this Room is the baseroom-
	 * @param n String name of room.
	 */
	Room(String n) {
		name = n;
		Config cfg = new Config();
		String base = cfg.getProperty("base");

		if (!base.equals(name)) {
			setThings();
		} else {
			nameAddition = "(base)";
		}

		setNeighbours();
	}

	/**
	 * runs addDoor() on each value separated by comma in property "doors" from Config
	 */
	private void setNeighbours() {
		Config cfg = new Config();
		String doorGet = cfg.getProperty("doors");
		String[] doorArray = doorGet.split(",");

		for (int i = 0; i < doorArray.length; i++) {
			addDoor(doorArray[i]);
		}
	}

	/**
	 * Seperates given value by "-" and check if one of these is equal to this Room name, and then add the other as neighbour.
	 * @param doorRow String row from doorArray in setNeighbours, on which to perform logic
	 */
	private void addDoor(String doorRow) {
		String[] tempD = doorRow.split("-");
		if (tempD[0].equals(name)) {
			if (neighbours.size() != 0) {
				if (!isAdded(tempD[1])) {
					neighbours.add(tempD[1]);
				}
			} else {
				neighbours.add(tempD[1]);
			}
		} else if (tempD[1].equals(name)) {
			if (neighbours.size() != 0) {
				if (!isAdded(tempD[0])) {
					neighbours.add(tempD[0]);
				}
			} else {
				neighbours.add(tempD[0]);
			}
		}

	}

	/**
	 * Check if given neighbour allrady exist, in the neighbour ArrayList of this Room object
	 * @param neighbour String, the name of Neighbour to check
	 * @return boolean true if exist, false if not.
	 */
	public boolean isAdded(String neighbour) {
		boolean isAdded = false;
		for (int y = 0; y < neighbours.size(); y++) {
			if (neighbours.get(y).equals(neighbour)) {
				isAdded = true;
			}
		}
		return isAdded;
	}

	/**
	 * Create a list of random Things in this Room object
	 */
	private void setThings() {

		Config cfg = new Config();
		String thingTypes = cfg.getProperty("things");
		String[] typeArray = thingTypes.split(",");

		Random r = new Random();
		String tempThing = "";

		for (int i = 0; i < r.nextInt(10) + 5; i++) {
			tempThing = typeArray[r.nextInt(typeArray.length)];

			things.add(i, new Thing(tempThing));
		}

	}

	/**
	 * Print out information about the Room name, list of neighbour rooms and a list of Things.
	 */
	public void lookAround() {
		console.println("\n[You are in] " + name + nameAddition);
		console.println("[Neigbour rooms] " + getNeighbours(false));
		console.println("[Things] " + getThings(false) + "\n");
	}

	/**
	 * Get a list of Things in this Room object.
	 * @param isMenu boolean, determines whether list should be shown as a comma separated(false) or as a list of options with number(true)
	 * @return String list of things contained by this Room
	 */
	public String getThings(boolean isMenu) {
		int max = things.size();
		String returnString = "";

		if (max == 0) {
			returnString = "empty";
		} else {
			returnString = neighbourList(max, isMenu, "things");
		}
		return returnString;
	}

	/**
	 * Get a list of neigbour rooms
	 * @param isMenu boolean, determines whether list should be shown as a comma separated(false) or as a list of options with number(true)
	 * @return String list of neighbours to this Room
	 */
	public String getNeighbours(boolean isMenu) {
		int max = neighbours.size();
		String returnString = "";
		if (max == 0) {
			returnString = "empty";
		} else {
			returnString = neighbourList(max, isMenu, "neighbours");
		}
		return returnString;
	}

	/**
	 * Creates the list for getNeighbours() and getThings()
	 * @param max int from either getNeighbours() and getThings()
	 * @param isMenu boolean from either getNeighbours() and getThings()
	 * @param type String "neighbours" or "things" to determine the output.
	 * @return String list of neighbours or things contained by this Room object
	 */
	private String neighbourList(int max, boolean isMenu, String type) {

		String returnString = "";
		for (int i = 0; i < max; i++) {
			if (type.equals("neighbours")) {
				if (isMenu) {
					returnString += "[" + i + "]" + neighbours.get(i) + "\n";
				} else {
					if (i == neighbours.size() - 1) {
						returnString += neighbours.get(i);
					} else {
						returnString += neighbours.get(i) + ", ";
					}
				}
			} else if (type.equals("things")) {
				if (isMenu) {
					returnString += "[" + i + "]" + things.get(i) + "\n";
				} else {
					if (i == things.size() - 1) {
						returnString += things.get(i).toString();
					} else {
						returnString += things.get(i).toString() + ", ";
					}
				}
			}

		}
		return returnString;
	}

	/**
	 * Get the name of the neighbour room, by the given index in the neighbours ArrayList
	 * @param position int Index in neighbours ArrayList
	 * @return String, name of neighbour room at given index in ArrayList
	 */
	public String getNeighbourAt(int position) {
		return neighbours.get(position);
	}

	/**
	 * Get the Thing object, by the given index in the things ArrayList
	 * @param position int Index in things ArrayList
	 * @return Thing at given index in things ArrayList
	 */
	public Thing getThingAt(int position) {
		return things.get(position);
	}

	/**
	 * Removes Thing object from given index in the things ArrayList
	 * @param position int Index in things ArrayList
	 */
	public void removeThingAt(int position) {
		things.remove(position);
	}

	/**
	 * Adds Thing object to the things ArrayList
	 * @param item Thing object to add
	 */
	public void addThing(Thing item) {
		things.add(item);
	}

	/**
	 * Overrides toString, and returns the Name of the Room object.
	 * @return String name of this Room
	 */
	@Override
	public String toString() {
		return name;
	}
}

